package com.ByteDance.controller;


import com.ByteDance.model.po.Message;
import com.ByteDance.model.po.UserInfos;
import com.ByteDance.model.vo.MessageVO;
import com.ByteDance.service.IMessageService;
import com.ByteDance.service.IUserService;
import com.ByteDance.utils.JwtUtils;
import com.ByteDance.utils.WebSocketSingleServer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jacoco.agent.rt.internal_43f5073.PreMain;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 聊天记录表 前端控制器
 * </p>
 *
 * @author 朱哥
 * @since 2024-03-06
 */
@Slf4j
@Api(tags = "消息Controller")
@RestController
@RequestMapping("/douyin/message")
@RequiredArgsConstructor
public class MessageController {

    private final WebSocketSingleServer webSocketSingleServer;
    private final IUserService iUserService;
    private final IMessageService iMessageService;

    @PostMapping("/action")
    @ApiOperation("消息操作接口")
    public MessageVO action(HttpServletRequest req,@RequestParam(value = "to_user_id") Integer toUserId,
                            @RequestParam(value = "action_type") Integer actionType,
                            @RequestParam(value = "content") String content){
        UserInfos toUser = iUserService.getById(toUserId);
        if (toUser == null){
            return MessageVO.fail();
        }
        int fromUserId = JwtUtils.getUserId(req.getHeader("token"));
        //将消息落库
        Message message = iMessageService.sendMsg(fromUserId, toUserId, actionType, content);
        log.info("消息message = {} " + message + "发送成功！");
        return MessageVO.success();
    }

    /**
     *
     * @param req
     * @param toUserId
     * @param preMsgTime 上次最新消息的时间
     * @return
     */

    @GetMapping
    @ApiOperation("/chat")
    public MessageVO chat(HttpServletRequest req, @RequestParam(value = "to_user_id") Integer toUserId,
                          @RequestParam(value = "pre_msg_time")String preMsgTime){
        if("0".equals(preMsgTime)) {
            Timestamp pre_msg_time = new Timestamp(System.currentTimeMillis());
        }else {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            try {
                Date date = dateFormat.parse(preMsgTime);
                Timestamp pre_msg_time = new Timestamp(date.getTime());
                System.out.println("Timestamp: " + pre_msg_time);
            } catch (ParseException e) {
                e.printStackTrace();
            }

        }


        UserInfos toUser = iUserService.getById(toUserId);
        if (toUser == null){
            return MessageVO.fail();
        }
        int fromUserId = JwtUtils.getUserId(req.getHeader("token"));
        List<Message> messages = iMessageService.getMsgList(fromUserId,toUserId,preMsgTime);
        Message[] messageList = messages.toArray(new Message[0]);//new Person[0]，实际上是为了告诉 toArray 方法返回一个新创建的与集合相同类型的空数组。
        MessageVO messageVO = MessageVO.success();
        messageVO.setMessageList(messageList);
        return messageVO;
    }
}
